package cjieba

import (
	"gitee.com/yysf_xin/go-common/tools"
	"github.com/blevesearch/bleve/v2/analysis"
	"github.com/blevesearch/bleve/v2/registry"
	"github.com/yanyiwu/gojieba"
)

var GoJieba = "gojieba"

func gojiebaConstructor(config map[string]interface{}, cache *registry.Cache) (*analysis.Analyzer, error) {
	tokenizer, err := cache.TokenizerNamed(GoJieba)
	if err != nil {
		return nil, err
	}

	az := &analysis.Analyzer{
		Tokenizer: tokenizer,
	}
	return az, nil
}

type goJeiba struct {
	j *gojieba.Jieba
}

func (g *goJeiba) Tokenize(bytes []byte) analysis.TokenStream {
	result := make(analysis.TokenStream, 0)
	rs := g.j.Tokenize(tools.BytesToString(bytes), gojieba.SearchMode, true)
	if len(rs) == 0 {
		return result
	}
	for i, v := range rs {
		if len(v.Str) < 2 {
			continue
		}
		token := analysis.Token{
			Term:     tools.StringToBytes(v.Str),
			Position: i,
			Type:     analysis.Ideographic,
		}
		result = append(result, &token)
	}
	return result
}

func goJiebaTokenizer(config map[string]interface{}, cache *registry.Cache) (analysis.Tokenizer, error) {
	j := gojieba.NewJieba()
	return &goJeiba{
		j: j,
	}, nil
}

func init() {
	registry.RegisterAnalyzer(GoJieba, gojiebaConstructor)
	registry.RegisterTokenizer(GoJieba, goJiebaTokenizer)
}
